package com.deliinsns.app.student;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Property;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.deliinsns.app.student.bean.Apply;
import com.deliinsns.app.student.bean.Student;
import com.deliinsns.sys.BasicController;
import com.deliinsns.sys.MsgType;
import com.deliinsns.sys.util.SystemConstant;
import com.deliinsns.sys.util.ParameterUtil;
@Controller
@RequestMapping("/student") 
public class StudentController extends BasicController {
	 @Autowired  
     private  StudentDao studentDao;  
	 @Autowired  
     private  ApplyDao applyDao;  
	 static Logger logger = Logger.getLogger(StudentController.class.getName());
	
	 @RequestMapping(value = "/list", method = RequestMethod.GET)
	 public void list(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String _first= request.getParameter("first");
		String _limit= request.getParameter("limit");
		String _name= request.getParameter("name");
		DetachedCriteria criteria = DetachedCriteria.forClass(Student.class);
		if(checkParam(_name)){
			criteria .add( Property.forName("name").like("%"+_name+"%") );  
		}
		int first =0;
		int limit=20;
		if(ParameterUtil.allNumeric(_first,_limit)){
			first = Integer.parseInt(_first);
			limit = Integer.parseInt(_limit);
		}
		List<Student> studentList= studentDao.findByCriteria(criteria, first, limit);
		msg.setState(200);
		msg.setMsgCode(SystemConstant.GET_SUCCESS);
		msg.setTotal(98);
		msg.setItems(studentList);
		out(request,response,MsgType.msg);
	}
	 
	@RequestMapping(value = "/get/{studentId}", method = RequestMethod.GET)
	 public void getStudent(@PathVariable String studentId ,HttpServletRequest request,HttpServletResponse response)throws Exception {
		if(checkParam(studentId)){				//如果所有参数都正确传入才进行相应处理
			Student student= studentDao.get(Student.class, studentId);
			  if(student!=null){
				  msg.setBean(student);
				  msg.setState(200);
				  msg.setMsg(SystemConstant.GET_SUCCESS);
			  }else{
				  msg.setMsg(SystemConstant.GET_FAILD);
			  }
		}else{
			msg.setMsg(SystemConstant.PARAM_ERROR);
		}
		out(request,response,MsgType.msg);
	 }
	
	
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	 public void saveStudent(Student student,HttpServletRequest request,HttpServletResponse response)throws Exception {
		String id =(student!=null)?student.getId():null;
		try{ 
			studentDao.saveOrUpdate(student);
		} 
		catch(Exception e) 
		{ 
			msg.setMsg(e.getMessage());
			out(request,response,MsgType.msg);
		}
		msg.setState(200);
		if(StringUtils.isNotEmpty(id))
			msg.setMsg(SystemConstant.UPDATE_SUCCESS);
		else
			msg.setMsg(SystemConstant.ADD_SUCCESS);
		out(request,response,MsgType.msg);
	 }
	
	@RequestMapping(value = "/student/delete/{studentIds}", method = RequestMethod.POST)
	 public void deleteStudent(@PathVariable String studentIds ,HttpServletRequest request,HttpServletResponse response)throws Exception {
		if(checkParam(studentIds)){				//如果所有参数都正确传入才进行相应处理
			try{
				String hql = "from Student m where m.id in"+"("+studentIds+")";
				List<Student> students= studentDao.findByHql(hql);
				studentDao.delete(students);
				msg.setState(200);
				msg.setMsg(SystemConstant.DELETE_SUCCESS);
			}catch(Exception e){
				msg.setMsg(e.getMessage());
				out(request,response,MsgType.msg);
			}
		}else{
			msg.setMsg(SystemConstant.PARAM_ERROR);
		}
		out(request,response,MsgType.msg);
	 }
	
	@RequestMapping(value = "/applyList", method = RequestMethod.GET)
	 public void applyList(HttpServletRequest request,HttpServletResponse response)throws Exception {
		String _first= request.getParameter("first");
		String _limit= request.getParameter("limit");
		String _name= request.getParameter("name");
		DetachedCriteria criteria = DetachedCriteria.forClass(Apply.class);
		if(checkParam(_name)){
			criteria .add( Property.forName("name").like("%"+_name+"%") );  
		}
		int first =0;
		int limit=20;
		if(ParameterUtil.allNumeric(_first,_limit)){
			first = Integer.parseInt(_first);
			limit = Integer.parseInt(_limit);
		}
		List<Apply> applyList= applyDao.findByCriteria(criteria, first, limit);
		System.out.println(applyList.size());
		msg.setState(200);
		msg.setMsg("successful!");
		msg.setTotal(98);
		msg.setItems(applyList);
		out(request,response,MsgType.msg);
	 }
}
